Every property in a NHibernate model must have a data type, which you configure through the Data Type drop-down in the Properties window. Most standard NHibernate data types and .NET primitive types are supported out of the box. If you need to add your own custom data types, you can do so via the NHibernate Model Explorer. Custom data types appear in the Data Type drop-down and can be selected for properties just like built-in data types.
To open the NHibernate Model window if it’s not already open, choose View > Other Windows > NHibernate Model. The window displays a tree view of your model.
Enums
To map an integer column as a user-defined enum type, right-click the Model node at the root of the tree view, and choose Add New Enum Type. Enter the following information:
- Name: The name by which you’d like to refer to the enum in the Data Type drop-down (e.g. Priority).
- CLR Type Name: The namespace-qualified name of the enum type (e.g. MyCompany.MyProject.Priority).
- Backing Data Type: How the enum is stored in the database. This is used only when synchronising between the model and the database, and can be ignored if you don’t plan to use that feature. It doesn’t affect the NHibernate mapping.
User Mapped Types
To map a column using a custom mapping (a NHibernate IUserType mapping), right-click the Model node at the root of the tree view, and choose Add New User Mapped Type. Enter the following information.
- Name: The name by which you’d like to refer to the data type in the Data Type drop-down (e.g. OuiNon).
- CLR Type Name: The namespace-qualified name of the CLR type to which the column is mapped. This could be a built-in type (e.g. System.Boolean) or a custom type (e.g. MyCompany.MyProject.Permissions).
- Mapping Type: The assembly-qualified name of the IUserType implementation which tells NHibernate how to map between the database column and the CLR type (e.g. MyCompany.MyProject.OuiNonMapping,MyCompany.MyProjectAssembly). Note that this name must be assembly-qualified. The mapping type must produce (from its NullSafeGet implementation) and consume (in its NullSafeSet implemention) the same type specified by CLR Type Name.
- Database Backing Data Type: How the custom type is stored in the database. This is used only when synchronising between the model and the database, and can be ignored if you don’t plan to use that feature. It doesn’t affect the NHibernate mapping.
- Database Backing Column Size: For custom types which are stored as strings in the database, the size of the database column. This is used only when creating the database schema from the model, and can be ignored if you don’t plan to use that feature.